package com.xpsoft.oa.workflow.event;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.admin.ProSealMake;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.admin.ProSealMakeService;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ProjectService;
import com.xpsoft.oa.service.system.AppUserService;

public class ProSealMakeFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(ProSealMakeFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String applyUserId = (String)pi.getVariable("proSealMake.applyUser.userId");
		String managerUserId = (String)pi.getVariable("proSealMake.managerUser.userId");
		String sealType = (String)pi.getVariable("proSealMake.sealType");
		String sealPurpose = (String)pi.getVariable("proSealMake.sealPurpose");
		String winBidNotice = (String)pi.getVariable("proSealMake.winBidNotice");
		String proId = (String)pi.getVariable("proSealMake.project.id");
		String sealDesc = (String)pi.getVariable("proSealMake.sealDesc");
		
		ProSealMake proSealMake = new ProSealMake();
		proSealMake.setSealPurpose(sealPurpose);
		proSealMake.setSealDesc(sealDesc);
		AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
		if(StringUtils.isNotBlank(applyUserId)){
			proSealMake.setApplyUser(appUserService.get(Long.valueOf(applyUserId)));
		}
		if(StringUtils.isNotBlank(managerUserId)){
			proSealMake.setManagerUser(appUserService.get(Long.valueOf(managerUserId)));
		}
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			proSealMake.setProject(projectService.get(Long.valueOf(proId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		proSealMake.setProcessRunId(processRun.getRunId());
		
		ProSealMakeService proSealMakeService = (ProSealMakeService)AppUtil.getBean("proSealMakeService");
		proSealMakeService.save(proSealMake);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
